/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.main;

import javax.sql.DataSource;

import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.google.common.collect.Lists;
import com.koron.workflow.api.listener.ProcessEventListener;

@Configuration
public class ProcessEngineConfig  {
	
	@Autowired
	@Qualifier("primaryDataSource")
	private DataSource dataSource;
	
	@Autowired
	private ApplicationContext applicationContext;
	
	@Bean
	public ProcessEventListener processEventListener() {
		return new ProcessEventListener();
	}
	
	@Bean
	public DataSourceTransactionManager transactionManager() {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean
	public SpringProcessEngineConfiguration processEngineConfiguration() {
		SpringProcessEngineConfiguration processEngine = new SpringProcessEngineConfiguration();
		processEngine.setDataSource(dataSource);
		processEngine.setTransactionManager(transactionManager());
		processEngine.setDatabaseSchemaUpdate("true");
		processEngine.setJobExecutorActivate(true);
		processEngine.setActivityFontName("宋体");
		processEngine.setJobExecutorActivate(false);
		processEngine.setLabelFontName("宋体");
		processEngine.setDatabaseSchema("ACT");
		processEngine.setEventListeners(Lists.newArrayList(new ProcessEventListener()));
		return processEngine;
	} 
	
	@Bean
	public ProcessEngineFactoryBean processEngineFactory() {
		ProcessEngineFactoryBean factory = new ProcessEngineFactoryBean();
		factory.setApplicationContext(applicationContext);
		factory.setProcessEngineConfiguration(processEngineConfiguration());
		return factory;
	}
	
	@Bean
	public ProcessEngine processEngine() throws Exception {
		return processEngineFactory().getObject();
	}
	
	@Bean
	public TaskService taskService() throws Exception {
		return processEngine().getTaskService();
	}
	
	@Bean
	public RuntimeService runtimeService() throws Exception {
		return processEngine().getRuntimeService();
	}
	
	@Bean
	public RepositoryService repositoryService() throws Exception {
		return processEngine().getRepositoryService();
	}
	
	@Bean
	public FormService formService() throws Exception {
		return processEngine().getFormService();
	}
	
	@Bean
	public HistoryService historyService() throws Exception {
		return processEngine().getHistoryService();
	}
	
	@Bean
	public ManagementService managementService() throws Exception {
		return processEngine().getManagementService();
	}
	
	@Bean
	public IdentityService identityService() throws Exception {
		return processEngine().getIdentityService();
	}
}
